//
//  ZipResource.m
//  Staff
//
//  Created by Marcus on 12/28/14.
//
//

#import "ZipResource.h"
#import "ChaosFile.h"
#import "ChaosFileFetcher.h"
#import "HashUtility.h"

@implementation ZipResource 

- (NSData *)fetchLocalZipWithFileName:(NSString *)fileName {
    ChaosFile *file = [[ChaosFile alloc] init];
    file.fileName = fileName;

    return [[ChaosFileFetcher sharedFetcher] getFile:file];
}

- (BOOL)compareCheckSum:(NSString *)checkSum {
    NSString *localFileCheckSum = [HashUtility getSHA512Hash:[self fetchLocalZipWithFileName:self.fileName]];
    if ([[localFileCheckSum lowercaseString] isEqualToString:[checkSum lowercaseString]]) {
        return YES;
    }
    return NO;
}

- (BOOL)needDownloadUpdate {
    return ![self compareCheckSum:self.hashCode];
}

- (BOOL)isUnzipped {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@",documentsDirectory,self.fileName]]) {
        return YES;
    }
    return NO;
}

@end
